package com.bytedance.scenedemo.migrate;

import com.bytedance.scene.Scene;
import com.bytedance.scene.SceneDelegate;
import com.bytedance.scene.interfaces.ChildSceneLifecycleAdapterCallbacks;
import com.bytedance.scene.interfaces.ChildSceneLifecycleCallbacks;
import com.bytedance.scene.ui.NavigationSceneCompatUtility;
import com.bytedance.scene.utlity.ViewIdGenerator;
import com.bytedance.scenedemo.ResourceTable;
import com.bytedance.scenedemo.group.viewpager.ViewPagerSample;
import ohos.aafwk.ability.fraction.Fraction;
import ohos.aafwk.ability.fraction.FractionAbility;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.LayoutScatter;

/**
 * GroupScene 绑定到 Fragment
 * Created by JiangQi on 9/5/18.
 */
public class GroupSceneBindToFragmentSample extends FractionAbility {

    @Override
    protected void onStart(Intent intent) {
        super.onStart(intent);
        setUIContent(ResourceTable.Layout_ablity_groupscene_bindtofragmentsample);
        getFractionManager().startFractionScheduler().add(
                ResourceTable.Id_groupSceneBindToFragmentSample, new AFragment()).submit();
    }

    public static class AFragment extends Fraction {
        private final int viewId = ViewIdGenerator.generateViewId();
        private SceneDelegate sceneDelegate;

        @Override
        protected Component onComponentAttached(LayoutScatter scatter, ComponentContainer container, Intent intent) {
            Component parse = scatter.parse(ResourceTable.Layout_fraction_afraction, container, false);
            parse.setId(viewId);
            return parse;
        }

        @Override
        protected void onStart(Intent intent) {
            super.onStart(intent);
            sceneDelegate = NavigationSceneCompatUtility.setupWithFragment(this,
                    ViewPagerSample.class, viewId).supportRestore(false).build();
            if (sceneDelegate == null){
                throw new NullPointerException("sceneDelegate is null!");
            }

            if (sceneDelegate.getNavigationScene() == null){
                throw new NullPointerException("sceneDelegate.getNavigationScene() is null!");
            }
            sceneDelegate.getNavigationScene().registerChildSceneLifecycleCallbacks((ChildSceneLifecycleCallbacks) (new ChildSceneLifecycleAdapterCallbacks(){
                @Override
                public void onSceneResumed(Scene scene) {
                    super.onSceneResumed(scene);
                }
            }),false);
        }
    }
}
